package com.business_clean.ui.activity.custom;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.Observer;

import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.data.mode.contact.CompanyContactEntity;
import com.business_clean.data.mode.contact.ContactListEntity;
import com.business_clean.data.mode.permission.PermissionData;
import com.business_clean.data.mode.project.ProjectMangerList;
import com.business_clean.databinding.ActivityAddCompanyBinding;
import com.business_clean.ui.activity.permissions.PermissionsActivity;
import com.business_clean.ui.activity.project.ProjectManagerActivity;
import com.business_clean.viewmodel.request.ProjectPersonnelViewModel;

import org.jetbrains.annotations.Nullable;

import java.util.HashMap;

import me.hgj.mvvmhelper.base.BaseViewModel;

public class AddCompanyActivity extends BaseActivity<ProjectPersonnelViewModel, ActivityAddCompanyBinding> {

    private String uuid;
    private String role_id;

    private String jump_type = "1";//1是公司，2是常用联系人

    private String project_uuid;//项目uuid

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        //需要区分一下，是添加常用联系人，还是公司的人
        if (getIntent() != null && getIntent().getExtras() != null) {
            uuid = getIntent().getExtras().getString("uuid");
            jump_type = getIntent().getExtras().getString("jump_type");//1是公司，2是常用联系人
            if ("1".equals(jump_type)) {
                hideCompanyView();
                if (!TextUtils.isEmpty(uuid)) {
                    mViewModel.requestCompanyContactDetail(uuid);
                    mDatabind.tabbar.setTitle("编辑总部成员");
                }
            } else {
                hideOtherView();
                if (!TextUtils.isEmpty(uuid)) {
                    mViewModel.requestContactDetail(uuid);
                    mDatabind.tabbar.setTitle("编辑常用联系人");
                }
            }
        } else {
            hideOtherView();
        }

        mDatabind.withAddPhone.getInputEdit().setInputType(InputType.TYPE_CLASS_NUMBER);
        mDatabind.withAddPhone.getInputEdit().setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
    }

    private void hideCompanyView() {
        mDatabind.tabbar.setTitle("添加总部成员");
        mDatabind.withAddProject.setVisibility(View.GONE);
        mDatabind.withAddPost.setVisibility(View.GONE);
        mDatabind.withAddName.setVisibility(View.VISIBLE);
        mDatabind.withAddPhone.setVisibility(View.VISIBLE);
        mDatabind.withAddPermission.setVisibility(View.VISIBLE);
    }

    private void hideOtherView() {
        mDatabind.tabbar.setTitle("添加常用联系人");
        mDatabind.withAddProject.setVisibility(View.VISIBLE);
        mDatabind.withAddPost.setVisibility(View.VISIBLE);
        mDatabind.withAddName.setVisibility(View.VISIBLE);
        mDatabind.withAddPhone.setVisibility(View.VISIBLE);
        mDatabind.withAddPermission.setVisibility(View.GONE);
        if (App.appViewModelInstance.getProjectInfo().getValue() != null) {
            mDatabind.withAddProject.setTvRightText(App.appViewModelInstance.getProjectInfo().getValue().getProject_short_name());
            project_uuid = App.appViewModelInstance.getProjectInfo().getValue().getUuid();
        }
    }

    @Override
    public void initObserver() {
        App.appViewModelInstance.getSelectPermissionsStatus().observe(this, new Observer<PermissionData>() {
            @Override
            public void onChanged(PermissionData data) {
                role_id = data.getId();
                mDatabind.withAddPermission.setTvRightText(data.getTitle());
            }
        });

        App.appViewModelInstance.getSelectCompanyProjectMangerList().observe(this, new Observer<ProjectMangerList>() {
            @Override
            public void onChanged(ProjectMangerList projectMangerList) {
                project_uuid = projectMangerList.getUuid();
                mDatabind.withAddProject.setTvRightText(projectMangerList.getProject_name());
            }
        });
    }

    @Override
    public void onBindViewClick() {

        mDatabind.withAddProject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(uuid)) {
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("isSelected", true);
                    ActivityForwardUtil.startActivity(ProjectManagerActivity.class, bundle);
                }
            }
        });
        mDatabind.withAddPermission.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("channel", "1");
                bundle.putString("selectText", "" + mDatabind.withAddPermission.getTvRight());
                ActivityForwardUtil.startActivity(PermissionsActivity.class, bundle);
            }
        });

        mDatabind.buttom.butBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mDatabind.withAddName.getInputText())) {
                    ToastUtil.show("请输入姓名");
                    return;
                }
                if (TextUtils.isEmpty(mDatabind.withAddPhone.getInputText())) {
                    ToastUtil.show("请输入手机号");
                    return;
                }

                if (TextUtils.isEmpty(role_id) && "1".equals(jump_type)) {
                    ToastUtil.show("请选择权限");
                    return;
                }
                if (TextUtils.isEmpty(mDatabind.withAddPost.getInputText()) && !"1".equals(jump_type)) {
                    ToastUtil.show("请输入岗位名称");
                    return;
                }
                if (TextUtils.isEmpty(project_uuid) && !"1".equals(jump_type)) {
                    ToastUtil.show("请选择项目");
                    return;
                }
                HashMap<String, String> hashMap = new HashMap<>();
                if (!TextUtils.isEmpty(uuid)) {
                    hashMap.put("uuid", "" + uuid);
                }
                if ("1".equals(jump_type)) {//总部成员
                    hashMap.put("project_uuid", "" + App.appViewModelInstance.getUserInfo().getValue().getProject().getUuid());
                    hashMap.put("user_name", "" + mDatabind.withAddName.getInputText());
                    hashMap.put("mobile", "" + mDatabind.withAddPhone.getInputText());
                    if (role_id != null) {
                        hashMap.put("role_id", "" + role_id);
                    }
                    mViewModel.requestSaveCompanyContact(hashMap);
                } else {//常用联系人
                    hashMap.put("person_name", "" + mDatabind.withAddName.getInputText());
                    hashMap.put("person_mobile", "" + mDatabind.withAddPhone.getInputText());
                    hashMap.put("job_name", "" + mDatabind.withAddPost.getInputText());
                    if (!TextUtils.isEmpty(project_uuid)) {
                        hashMap.put("project_uuid", "" + project_uuid);
                    }
                    mViewModel.requestSaveContact(hashMap);
                }

            }
        });
    }

    @Override
    public void onRequestSuccess() {
        mViewModel.getCompanyContactDetail().observe(this, new Observer<CompanyContactEntity>() {
            @Override
            public void onChanged(CompanyContactEntity data) {
                fillCompanyData(data);
            }
        });

        mViewModel.getContactDetail().observe(this, new Observer<ContactListEntity>() {
            @Override
            public void onChanged(ContactListEntity contactListEntity) {
                fillData(contactListEntity);
            }
        });

        mViewModel.getSaveContactUpdate().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                ToastUtil.show("成功");
                App.appViewModelInstance.getRefreshContactManagerList().setValue(true);
                finish();
            }
        });
    }

    //常用联系人
    private void fillData(ContactListEntity data) {
        if (data != null) {
            uuid = data.getUuid();
            mDatabind.withAddName.setInputText(data.getPerson_name());
            mDatabind.withAddPhone.setInputText(data.getPerson_mobile());
            mDatabind.withAddPost.setInputText(data.getJob_name());
            mDatabind.withAddProject.setTvRightText(data.getProject_name());
        }
    }

    //总部成员
    private void fillCompanyData(CompanyContactEntity data) {
        if (data != null) {
            uuid = data.getUuid();
            role_id = data.getRole_id();
            mDatabind.withAddName.setInputText(data.getUser_name());
            mDatabind.withAddPhone.setInputText(data.getMobile());
            mDatabind.withAddPermission.setTvRightText(data.getRole_name());
        }
    }
}